<?php

function printHeader($title)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
      <title>GranBazar! - <?echo $title;?></title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <meta content="">
      <link type="text/css" rel="stylesheet" href="style.css">
      <?
         if(isset($_SESSION['usrpasserr']))
         {
            if($_SESSION['usrpasserr'])
            {
               ?>
                  <script language="Javascript">
                     function usrPassErr()
                     {
                        alert('Attenzione! Hai inserito Username e/o Password errati!')
                     }
                  </script>
               <?
            }
         }
      ?>
   </head>
         <body
               <?
               if($_SESSION['usrpasserr'])
               {
                  ?> onLoad="usrPassErr()"<?
                  session_unregister('usrpasserr');
               }
               ?>
         >
      <div id="global">
         <div id="header">
            <?
               printLogin();
            ?>
            <div class="title"><a href="index.php">GranBazar!</a></div>
            <div class="subtitle"> <?echo $title ?> </div>
            <?printFormSearch("");?>
         </div>
         <div id="middle">

         <?
}

function printLogin()
{
   ?>
      <div class="login">
   <?
   if(isset($_SESSION['logged']))
   {
     if($_SESSION['logged'])
     {
      ?>
         <a title="Premi per sloggarti"
            href="<?echo basename($_SERVER['PHP_SELF']);?>?logout=true">Logout [ <?echo $_SESSION['user']?> ]</a>
      <?
     }
   }
   else
   {
      printFormLogin();
   }
   ?>
      </div>
   <?
}

function printFormSearch()
{
  ?>
   <div class="search">
      <form action="search.php" method="POST">
      <input type="text" name="searchtext"  maxlength="50" value="<?if(isset($_POST['searchtext'])) echo $_POST['searchtext'];?>">
      <input class="submit" type="submit" value="Ricerca">
      </form>
   </div>
  <?
}




function printFormLogin()
{
   ?>
      <form action="<?echo basename($_SERVER['PHP_SELF']);?>" method="POST">
      <span>Username:
            <input type="text" name="username" size="10" >
         </span>
         <span>Password:
            <input type="password" name="password" size="10">
         </span>
         <span>
            <input class="submit" type="submit" value="Login"><br/>
       <a href="registrazione.php" title="Registrati se non l'hai ancora fatto!">[ Registrazione ]</a>
         </span>
      </form>
   <?
}

function printFooter()
{
?>
   </div>
   <div id="footer">
      Sito realizzato da Federico Ficarelli e Marco Pagliari per l'esame di <i>"Laboratorio di Basi di Dati"</i>
   </div>
  </div>
</body>
</html>
<?
}


function printLink()
{
  ?>
  <div class="linkbar">
   <h3>Collegamenti</h3>
  <?
   printDefaultLink();

  if($_SESSION['logged'])
  {
   printRegisterLink();
  }

  if(strcmp($_SESSION['type'],"admin") == 0)
  {
   printAdminLink();
  }
  ?>

  </div>
  <?
}

function printDefaultLink()
{
?>
   <div class="defaultLink">
    <a href="asta.php">Aste</a> <br/>
    <a href="cat.php">Categorie</a>
   </div>
<?
}

function printRegisterLink()
{
?>
   <div class="registeredLink">
    <h3>Personale</h3>
      <a href="user.php?idus=<?echo $_SESSION['id'];?>">Profilo</a><br/>
      <a href="offering.php">Offerte</a><br/>
      <a href="sell.php">Vendita</a>
   </div>

<?
}

function printAdminLink()
{
?>
  <div class="adminLink">
    <h3>Admin</h3>
      <a href="adminUser.php">Utenti</a><br/>
      <a href="adminAste.php">Aste concluse</a><br/>
      <a href="adminCat.php">Categorie</a><br/>
      <a href="adminHistory.php?ush=true&show=limit&start=0&num=10">Storico</a><br/>
      <!--<a href="adminSite.php">Configurazione</a><br/>-->
  </div>


<?
}

function printRootCategories()
{
   ?>
   <div class="categories">
   <?

   $cat=dbGetRootCategories();

   echo "<h3><a href=\"cat.php\">Categorie</a></h3> Sono presenti ".dbCountCategoryObjects(NULL)." oggetti.<br><br>";
   while($riga = mysql_fetch_array($cat))
   {
      echo "<a href=\"cat.php?idcat=".$riga["id"]."\">".$riga["nome"]."</a><br/>";
   }
   ?>
   </div>
   <?
}

// Dato username e reputazione,
// formatta il link con nome utente,
// link alla pagina dell'utente e
// stellina della reputazione.
// Utile per uniformare la visualizzazione
// dei nomi utente
// (pensavo che in futuro, a seconda della reputazione
// la stellina potrebbe avere un colore diverso: taroccando
// questa funzione la modifica si propaga automaticamente)
function getUserLink($userid, $username, $rept)
{
  $userdata = $username." <nobr>(".$rept;
  $userdata .= getLittleStar($rept);
  $userdata .=")</nobr>";
  $userdata = "<a href=\"user.php?idus=$userid\">".$userdata."</a>";
  return $userdata;
}


function printObjectImageTag($id_oggetto)
{
  echo "<img src=\"show_image.php?objid=".$id_oggetto."\" ".dbGetImageDimensionStringFromObject($id_oggetto)."/>";
}

function printObjectImageTagResize($id_oggetto, $max_width, $max_height)
{
  $dims = dbGetImageDimensionsFromObject($id_oggetto);

  $width = $dims[0] > $max_width ? $max_width : $dims[0];
  $height = $dims[1] > $max_height ? $max_height : $dims[1];
  echo "<img src=\"show_image.php?objid=".$id_oggetto."\" width=\"".$width."\" height=\"".$height."\" />";
}


function printImageTag($id_immagine)
{
  echo "<img src=\"show_image.php?imgid=".$id_oggetto."\" ".dbGetImageDimensionString($id_oggetto)."/>";
}

function printImageTagResize($id_immagine, $width, $height)
{
  $dims = dbGetImageDimensions($id_immagine);

  $width = $dims[0] > $max_width ? $max_width : $dims[0];
  $height = $dims[1] > $max_height ? $max_height : $dims[1];
  echo "<img src=\"show_image.php?objid=".$id_oggetto."\" width=\"".$width."\" height=\"".$height."\" />";
}

function getStar($reputazione)
{
  switch($reputazione)
  {
    case 0: return '<img src="img/star0.gif">';
    break;
    case 1: return '<img src="img/star1.gif">';
    break;
    case 2: return '<img src="img/star2.gif">';
    break;
    case 3: return '<img src="img/star3.gif">';
    break;
    case 4: return '<img src="img/star4.gif">';
    break;
    case 5: return '<img src="img/star5.gif">';
    break;
    default:return '<img src="img/star0.gif">';
    break;
  }
}

function getLittleStar($reputazione)
{
  switch($reputazione)
  {
    case 0: return '<img src="img/littlestar0.gif">';
    break;
    case 1: return '<img src="img/littlestar1.gif">';
    break;
    case 2: return '<img src="img/littlestar2.gif">';
    break;
    case 3: return '<img src="img/littlestar3.gif">';
    break;
    case 4: return '<img src="img/littlestar4.gif">';
    break;
    case 5: return '<img src="img/littlestar5.gif">';
    break;
    default:return '<img src="img/littlestar0.gif">';
    break;
  }
}

function getRootCategories()
{
  $root = dbGetSubCategories(null);
  while($sub=mysql_fetch_array($root))
  {
    $id=$sub['id'];
    $num_oggetti = dbCountAllCategoryObjects($id);
    ?>
      <tr>
      <td class="td0">
      <a href="adminCat?idcat=<?echo $id;?>"><?echo dbGetCategoryName($id)." (".$num_oggetti.")";?></a>
      </td>
      <td>
          <a href="adminCat?addcat=<?echo $id;?>">Aggiungi</a>
      </td>
      <td>
          <a href="adminCat?modcat=<?echo $id;?>">Modifica</a>
      </td>
      <?if($num_oggetti==0){?>
      <td>
          <a href="adminCat?show=root&remcat=<?echo $id;?>">Elimina</a>
      </td>
      <?}else{?><td></td><?}?>
      </tr>
    <?
  }
}


function GetAllCategories($id)
{
  $level = 0;
  if($id != null)
  {
    $num_oggetti = dbCountAllCategoryObjects ($id);

    ?>
    <tr>
    <td class="td0">
        <a href="adminCat?idcat=<?echo $id;?>"><?echo dbGetCategoryName($id)." (".$num_oggetti.")";?></a>
    </td>
    <td>
      <a href="adminCat?addcat=<?echo $id;?>">Aggiungi</a>
    </td>
    <td>
      <a href="adminCat?modcat=<?echo $id;?>">Modifica</a>
    </td>
      <?if($num_oggetti==0){?>
      <td>
          <a href="adminCat?remcat=<?echo $id;?>">Elimina</a>
      </td>
      <?}else{?><td></td><?}?>
    </tr>
   <?
   $level=1;
  }
  $root = dbGetSubCategories($id);
  while($sub=mysql_fetch_array($root))
  {
    printSubCategories($sub['id'],$level);
  }
}

function printSubCategories($id,$level)
{
  $sub = dbGetSubCategories($id);
  echo "<tr>";

  printLevelCat($id,$level);

  while($row=mysql_fetch_array($sub))
  {
    printSubCategories($row['id'],$level+1);
  }
  echo "</tr>";
}


function printLevelCat($id,$level)
{
  $num_oggetti = dbCountAllCategoryObjects($id);
  ?>
      <td class="td<?echo $level%4?>" style="border-left-width:<?echo($level+1)*20;?>px;">
      <a href="adminCat?idcat=<?echo $id;?>"><?echo dbGetCategoryName($id)." (".$num_oggetti.")";?></a>
      </td>
      <td>
          <a href="adminCat?addcat=<?echo $id;?>">Aggiungi</a>
      </td>
      <td>
          <a href="adminCat?modcat=<?echo $id;?>">Modifica</a>
      </td>
      <?if($num_oggetti==0){?>
      <td>
          <a href="adminCat?remcat=<?echo $id;?>">Elimina</a>
      </td>
      <?}else{?><td></td><?}?>
  <?
}

function chooseRootCategories()
{
  $root = dbGetSubCategories(null);
  while($sub=mysql_fetch_array($root))
  {
    $id=$sub['id'];
    $num_oggetti = dbCountSubCategories($id);
    ?>
      <tr>
      <td class="td0">
      <a href="sell_cat?categoria=<?echo $id;?>"><?echo dbGetCategoryName($id)." (".$num_oggetti.")";?></a>
      </td>
      <td>
          <a href="sell_check?categoria=<?echo $id;?>">Scegli</a>
      </td>
      </tr>
    <?
  }
}


function chooseAllCategories($id)
{
  $level = 0;
  if($id != null)
  {
    $num_oggetti = dbCountSubCategories($id);

    ?>
    <tr>
    <td class="td0">
        <a href="sell_cat?categoria=<?echo $id;?>"><?echo dbGetCategoryName($id)." (".$num_oggetti.")";?></a>
    </td>
    <td>
      <a href="sell_check.php?categoria=<?echo $id;?>">Scegli</a>
    </td>
    </tr>
   <?
   $level=1;
  }
  $root = dbGetSubCategories($id);
  while($sub=mysql_fetch_array($root))
  {
    chooseSubCategories($sub['id'],$level);
  }
}

function chooseSubCategories($id,$level)
{
  $sub = dbGetSubCategories($id);
  echo "<tr>";

  chooseLevelCat($id,$level);

  while($row=mysql_fetch_array($sub))
  {
    chooseSubCategories($row['id'],$level+1);
  }
  echo "</tr>";
}

function chooseLevelCat($id,$level)
{
  $num_oggetti = dbCountSubCategories($id);
  ?>
      <td class="td<?echo $level%4?>" style="border-left-width:<?echo($level+1)*20;?>px;">
      <a href="sell_cat?categoria=<?echo $id;?>"><?echo dbGetCategoryName($id)." (".$num_oggetti.")";?></a>
      </td>
      <td>
          <a href="sell_check?categoria=<?echo $id;?>">Scegli</a>
      </td>
  <?
}

?>